
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author USER
 */
public class GrassCanvas extends GameCanvas implements Runnable {

    //path
    private String pathShadow = "";
    private String pathJahatShadow = "";
    private String pathShuriken = "";
    private String pathShurikenBox = "";
    private String pathBtnClose = "";
    private String pathBtnResume = "";
    private String pathBtnShuriken = "";
    private String pathBgGrass = "";
    private String pathBgDessert = "";
    //private String pathBgSnow = "";
    //animated sprite
    private AnimatedSprite spShadow;
    private AnimatedSprite spJahatShadow1;
    private static int FRAME_SHADOW_WIDTH = 0;
    private static int FRAME_SHADOW_HEIGHT = 0;
    //private AnimatedSprite spShuriken;
    private static int FRAME_SHURIKEN_WIDTH = 0;
    private static int FRAME_SHURIKEN_HEIGHT = 0;
    private BackgroundScroll bgScroll;
    //non animated sprite
    private ShurikenBoxSprite spShurikenBox;
    private LayerManager lmgr;
    private boolean running = false;
    private NinjaRunMIDlet midlet;
    //canvas property
    private final int CANVAS_WIDTH = getWidth();
    private final int CANVAS_HEIGHT = getHeight();
    private int bgWidth = 0;
    //private int warna;
    //button
    private ButtonImage btnClose;
    private ButtonImage btnResume;
    private Image imgBtnClose;
    private Image imgBtnResume;
    //ButtonImage btnShuriken;
    //Image imgBtnShuriken;
    //positions
    private int x = 0;
    private int y = 0;
    private int xBg = 0;
    private int yBg = 0;
    private int xShadow = 0;
    private int yShadow = 0;
    private int xJahatShadow1 = 0;
    private int yJahatShadow1 = 0;
    private int xShuriken1 = 0;
    private int yShuriken1 = 0;
    private int yPlatform = 0;
    private int tinggiJump = 0;
    private int tinggiJump2 = 0;
    //speed 
    private int speedGame = 50;
    private int speedBg = 5;
    private int speedShadow = 5;
    private int speedShadowJahat1 = 5;
    private int speedShuriken = 10;
    //conditions
    private boolean isJump = false;
    private boolean isJump2 = false;
    private boolean isDown = false;
    private boolean isTickBlash = false;
    private boolean isKena = false;
    private boolean isShurikenKenaMusuh = false;
    private boolean isAttack = false;
    private boolean isPaused = false;
    Thread thread = null;

    public GrassCanvas(NinjaRunMIDlet midlet) {
        super(true);

        //warna = 0xffffff;
        this.midlet = midlet;
        /*
        if (CANVAS_WIDTH < CANVAS_HEIGHT) {
        //portrait
        yBg = CANVAS_HEIGHT - 240;
        } else {
        //landscape
        yBg = 0;
        }
         * 
         */

        if (CANVAS_WIDTH == 640) {
            FRAME_SHADOW_WIDTH = 46;
            FRAME_SHADOW_HEIGHT = 49;
            FRAME_SHURIKEN_WIDTH = 16;
            FRAME_SHURIKEN_HEIGHT = 16;
            pathShadow = "/black-shadow640.png";
            pathJahatShadow = "/jahat-shadow640.png";
            pathShuriken = "/shuriken640.png";
            pathShurikenBox = "/shurikenbox640.png";
            pathBtnClose = "/close640.png";
            pathBtnResume = "/btnResume640.png";
            pathBtnShuriken = "/btnShuriken640.png";
            pathBgGrass = "/bg-grass640.png";
            pathBgDessert = "/bg-dessert640.png";
            //pathBgSnow = "/bg-snow640.png";
            bgWidth = 1280;
        } else {
            FRAME_SHADOW_WIDTH = 23;
            FRAME_SHADOW_HEIGHT = 25;
            FRAME_SHURIKEN_WIDTH = 8;
            FRAME_SHURIKEN_HEIGHT = 8;
            pathShadow = "/black-shadow320.png";
            pathJahatShadow = "/jahat-shadow320.png";
            pathShuriken = "/shuriken320.png";
            pathShurikenBox = "/shurikenbox320.png";
            pathBtnClose = "/close320.png";
            pathBtnResume = "/btnResume320.png";
            pathBtnShuriken = "/btnShuriken320.png";
            pathBgGrass = "/bg-grass320.png";
            pathBgDessert = "/bg-dessert320.png";
            //pathBgSnow = "/bg-snow320.png";
            bgWidth = 640;
        }
        tinggiJump = (int) ((CANVAS_HEIGHT * 0.65));
        tinggiJump2 = (int) ((CANVAS_HEIGHT * 0.45));
        xShadow = (int) ((CANVAS_WIDTH * 0.2));
        yShadow = (int) ((CANVAS_HEIGHT * 0.8));
        yPlatform = (int) ((CANVAS_HEIGHT * 0.8));

        xJahatShadow1 = CANVAS_WIDTH;
        yJahatShadow1 = (int) ((CANVAS_HEIGHT * 0.8));

        try {
            // nonanimated sprites
            spShurikenBox = new ShurikenBoxSprite(Image.createImage(pathShurikenBox));

            //pilih background
            bgScroll = new BackgroundScroll(Image.createImage(pathBgGrass));
            bgScroll.setRefPixelPosition(xBg, yBg);

            //animated sprite
            spShadow = new AnimatedSprite(Image.createImage(pathShadow),
                    FRAME_SHADOW_WIDTH, FRAME_SHADOW_HEIGHT);
            spShadow.defineReferencePixel(FRAME_SHADOW_WIDTH / 2, FRAME_SHADOW_HEIGHT / 2);
            spShadow.setRefPixelPosition(xShadow, yShadow);

            spJahatShadow1 = new AnimatedSprite(Image.createImage(pathJahatShadow),
                    FRAME_SHADOW_WIDTH, FRAME_SHADOW_HEIGHT);
            spJahatShadow1.defineReferencePixel(FRAME_SHADOW_WIDTH / 2, FRAME_SHADOW_HEIGHT / 2);
            spJahatShadow1.setRefPixelPosition(xJahatShadow1, yJahatShadow1);

            /*
            spShuriken = new AnimatedSprite(Image.createImage(pathShuriken),
            FRAME_SHURIKEN_WIDTH, FRAME_SHURIKEN_HEIGHT);
            spShuriken.defineReferencePixel(FRAME_SHURIKEN_WIDTH / 2,
            FRAME_SHURIKEN_HEIGHT / 2);
            
            xShuriken1 = xShadow;
            yShuriken1 = yShadow;
            spShuriken.setRefPixelPosition(xShuriken1, yShuriken1);
             * 
             */

            // create and add to layer manager
            lmgr = new LayerManager();
            lmgr.append(spShadow);
            lmgr.append(spJahatShadow1);
            lmgr.append(spShurikenBox);
            //lmgr.append(spShuriken);
            lmgr.append(bgScroll);
            //spShuriken.setVisible(false);

            //button
            imgBtnClose = Image.createImage(pathBtnClose);
            imgBtnResume = Image.createImage(pathBtnResume);

            int xyArea[] = new int[4];
            int wClose = 0;//getWidth() - imgMineIdOff.getWidth();
            int hClose = 0;//getHeight() - imgBtnClose.getHeight();
            xyArea[0] = hClose; //x1
            xyArea[1] = hClose + imgBtnClose.getWidth(); //x2
            xyArea[2] = wClose; //y1
            xyArea[3] = wClose + imgBtnClose.getHeight(); //y2
            btnClose = new ButtonImage("close", imgBtnClose, imgBtnClose,
                    imgBtnClose, wClose,
                    hClose, xyArea, Graphics.TOP | Graphics.LEFT);

            int xResume = CANVAS_WIDTH - imgBtnResume.getWidth();
            int yResume = CANVAS_HEIGHT - imgBtnResume.getHeight();
            xyArea[0] = xResume; //x1
            xyArea[1] = xResume + imgBtnResume.getWidth(); //x2
            xyArea[2] = yResume; //y1
            xyArea[3] = yResume + imgBtnResume.getHeight(); //y2
            btnResume = new ButtonImage("resume", imgBtnResume, imgBtnResume,
                    imgBtnResume, xResume,
                    yResume, xyArea, Graphics.TOP | Graphics.LEFT);
            /*
            int wShuriken = getWidth() - imgBtnShuriken.getWidth();
            int hShuriken = getHeight()/2;//getHeight() - imgBtnClose.getHeight();
            xyArea[0] = hShuriken; //x1
            xyArea[1] = hShuriken + imgBtnShuriken.getWidth(); //x2
            xyArea[2] = wShuriken; //y1
            xyArea[3] = wShuriken + imgBtnShuriken.getHeight(); //y2
            btnShuriken = new ButtonImage("shuriken", imgBtnShuriken, 
            imgBtnShuriken, imgBtnShuriken, wClose,
            hClose, xyArea, Graphics.TOP | Graphics.LEFT);
             * 
             */
        } catch (Exception e) {
        }
    }

    protected void sizeChanged(int w, int h) {
        //super.sizeChanged(w, h);
    }

    //start thread
    public void start() {
        if (!isPaused) {
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            // look for keypress
            System.out.println(yShadow);
            //System.out.println(tinggiJump2);
            //btnResume.paint(g);
            if (yShadow > tinggiJump2) {
                checkForKeys();
            }

            checkForCollision();
            if (!isKena) {
                drawDisplay(g);
            } else {
                //flash backlight and vibrate device
                midlet.display.flashBacklight(500);
                midlet.display.vibrate(500);
                drawDisplay(g);
                isKena = false;
            }

            /*
            checkForCollision();
            if (!isShurikenKenaMusuh) {
            //System.out.println("Kena!");
            drawDisplay(g);
            //spJahatShadow1.setVisible(false);
            //xJahatShadow1 = CANVAS_WIDTH;
            //spJahatShadow1.setPosition(CANVAS_WIDTH + 100, yJahatShadow1);
            
            } else {
            isShurikenKenaMusuh = false;
            }
             * 
             */
            //System.out.println(spShuriken.getX());
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }

        }
    }

    protected void keyPressed(int keyCode) {

        if (!isPaused) {
            if (keyCode == -6) {
                midlet.showMenuCanvas(false);
            }
        } else {
            btnResume.setPositionXY(CANVAS_HEIGHT, CANVAS_WIDTH,
                    Graphics.TOP | Graphics.LEFT);
            isPaused = false;
            start();
        }
    }

    private void checkForKeys() {
        int keyState = getKeyStates();

        if ((keyState & FIRE_PRESSED) != 0) {
            isAttack = true;
        } else if ((keyState & LEFT_PRESSED) != 0) {
            //spMan.moveLeft();
        } else if ((keyState & RIGHT_PRESSED) != 0) {
            //spMan.moveRight(CANVAS_WIDTH);
        } else if ((keyState & UP_PRESSED) != 0) {
            checkJump();
        } else if ((keyState & DOWN_PRESSED) != 0) {
            //spMan.moveDown(CANVAS_HEIGHT);
        } else if ((keyState & GAME_A_PRESSED) != 0) {
            //System.out.println("A!!!");
        } else if ((keyState & GAME_B_PRESSED) != 0) {
            //System.out.println("B!!!");
        } else if ((keyState & GAME_C_PRESSED) != 0) {
            //System.out.println("C!!!");
        } else if ((keyState & GAME_D_PRESSED) != 0) {
            //System.out.println("D!!!");
        }
    }

    private void checkForCollision() {
        if (spShadow.collidesWith(spJahatShadow1, true)) {
            //|| spShadow.collidesWith(spJahatShadow1, true)){
            //|| spShadow.collidesWith(spCube, true)) {
            //|| spMan.collidesWith(spStar, true)) {
            //upon collision, restore the last x/y

            //spMan.restoreXY();
            isKena = true;
        } else {
            isKena = false;
        }

        /*
        if (spShuriken.collidesWith(spJahatShadow1, true)) {
        System.out.println("beunang!");
        isShurikenKenaMusuh = true;
        } else {
        isShurikenKenaMusuh = false;
        }
         * 
         */
    }

    private void drawDisplay(Graphics g) {
        setFullScreenMode(true);
        //clear background
        // g.setColor(warna);
        //g.fillRect(0, 0, getWidth(), getHeight());

        if (xJahatShadow1 < 0) {
            xJahatShadow1 = CANVAS_WIDTH + 50;
        }

        if (isJump2) {
            if (yShadow < tinggiJump2) {
                isDown = true;
                isJump = false;
                isJump2 = false;
                yShadow = yShadow + speedShadow;
            } else {
                if (isTickBlash) {
                    spShadow.setFrame(1);
                    spShadow.setFrame(2);
                    spShadow.setFrame(3);
                    isTickBlash = false;
                } else {
                    spShadow.setFrame(0);
                    yShadow = yShadow - speedShadow;
                }
            }
            //
        } else if (isJump) {
            if (yShadow < tinggiJump) {
                isDown = true;
                isJump = false;
            }
            yShadow = yShadow - speedShadow;
            spShadow.setFrame(0);
        } else if (isDown) {
            if (yShadow > yPlatform) {
                yShadow = yPlatform;
                isDown = false;
                isJump = false;
            } else {
                yShadow = yShadow + speedShadow;
                spShadow.setFrame(2);
            }
        } else {
            spShadow.nextFrame();
        }
        spShadow.setPosition(xShadow, yShadow);

        if (xBg == -bgWidth) {
            xBg = 0;
        } else {
            xBg = xBg - speedBg;
            xJahatShadow1 = xJahatShadow1 - speedShadowJahat1;
        }
        bgScroll.setPosition(xBg, yBg);

        /*
        if (isAttack) {
        spShuriken.setVisible(true);
        if(xShuriken1>CANVAS_WIDTH){
        xShuriken1 = xShadow;
        isAttack = false;
        }else{
        xShuriken1 = xShuriken1 + speedShuriken;
        }
        } else {
        //spShuriken.setVisible(false);
        //xShuriken1 = xShadow;
        
        }
        spShuriken.setPosition(xShuriken1, yShuriken1);
        spShuriken.nextFrame();
         * 
         */

        spJahatShadow1.setPosition(xJahatShadow1, yJahatShadow1);
        spJahatShadow1.nextFrame();

        //paint all layers
        lmgr.paint(g, 0, 0);
        //btnShuriken.paint(g);
        btnClose.paint(g);
        //btnResume.paint(g);
        g.setColor(0x0000ff);
        g.drawString(CANVAS_WIDTH + "x" + CANVAS_HEIGHT,
                CANVAS_WIDTH / 2, getHeight() / 2, Graphics.TOP | Graphics.LEFT);

        //flush off-screen buffer to display
        flushGraphics();
    }

    public void stop() {
        running = false;
    }

    public void paused() {
        isPaused = true;
        running = false;
    }

    protected void pointerPressed(int x, int y) {
        this.x = x;
        this.y = y;
        if (x > btnResume.xImage
                && x < (btnResume.xImage + btnResume.imageWidth)
                && y > btnResume.yImage
                && y < (btnResume.yImage + btnResume.imageHeight)) {
            System.out.println("OI!");
            keyPressed(-7);
            repaint();
        }

        if (x > btnClose.xImage
                && x < (btnClose.xImage + btnClose.imageWidth*2)
                && y > btnClose.yImage
                && y < (btnClose.yImage + btnClose.imageHeight*2)) {
            btnClose.pointerPressed(x, y);
            midlet.showMenuCanvas(false);            
        } else {
            if (!isPaused) {
                checkJump();
            }
        }
        
    }
    private void checkJump() {
        if (!isJump) {
            isJump = true;
            isJump2 = false;
            isDown = false;
            //System.out.println("1!");
        } else {
            if (yShadow < tinggiJump2) {
                System.out.println("lewat batas!!");
            } else {
                System.out.println("beluum");
                isJump2 = true;
                isTickBlash = true;
            }
        }
    }

    protected void hideNotify() {
        super.hideNotify();
        paused();
        int xResume = CANVAS_WIDTH - imgBtnResume.getWidth();
        int yResume = CANVAS_HEIGHT - imgBtnResume.getHeight();

        btnResume.setPositionXY(xResume, yResume,
                Graphics.TOP | Graphics.LEFT);
    }

    protected void showNotify() {
        super.showNotify();
        if (isPaused) {
            Graphics g = getGraphics();
            btnResume.paint(g);
            //checkForKeys();
        }
    }
}
